<template>
  <a-scrollbar ref="scrollContainer" v-on:mousewheel="handleMouseWheel" class="scroll-container">
    <div class="contentWidth">
      <slot />
    </div>
  </a-scrollbar>
</template>
<script setup>
import { useTagsViewStore } from '@/store/modules/tagsView'
import { getCurrentInstance, ref, onMounted, computed, onBeforeUnmount } from 'vue'

const tagAndTagSpacing = ref(4)
const { proxy } = getCurrentInstance()

const emits = defineEmits(['scroll'])
const emitScroll = () => {
  emits('scroll')
}

const tagsViewStore = useTagsViewStore()
const scrollWrapper = computed(() => proxy?.$refs.scrollContainer.$refs.containerRef)

onMounted(() => {
  scrollWrapper.value.addEventListener('scroll', emitScroll, true)
})
onBeforeUnmount(() => {
  scrollWrapper.value.removeEventListener('scroll', emitScroll)
})

function moveToTarget(currentTag) {
  const $container = proxy.$refs.scrollContainer.$el
  const $containerWidth = $container.offsetWidth
  const $scrollWrapper = scrollWrapper.value

  let firstTag = null
  let lastTag = null

  // find first tag and last tag
  if (tagsViewStore.visitedViews.length > 0) {
    firstTag = tagsViewStore.visitedViews[0]
    lastTag = tagsViewStore.visitedViews[tagsViewStore.visitedViews.length - 1]
  }

  if (firstTag === currentTag) {
    $scrollWrapper.scrollLeft = 0
  } else if (lastTag === currentTag) {
    $scrollWrapper.scrollLeft = $scrollWrapper.scrollWidth - $containerWidth
  } else {
    const tagListDom = document.getElementsByClassName('tags-item')
    const currentIndex = tagsViewStore.visitedViews.findIndex((item) => item === currentTag)
    let prevTag = null
    let nextTag = null
    for (const k in tagListDom) {
      if (k !== 'length' && Object.hasOwnProperty.call(tagListDom, k)) {
        if (tagListDom[k].dataset.path === tagsViewStore.visitedViews[currentIndex - 1].path) {
          prevTag = tagListDom[k]
        }
        if (tagListDom[k].dataset.path === tagsViewStore.visitedViews[currentIndex + 1].path) {
          nextTag = tagListDom[k]
        }
      }
    }

    // the tag's offsetLeft after of nextTag
    const afterNextTagOffsetLeft = nextTag.offsetLeft + nextTag.offsetWidth + tagAndTagSpacing.value

    // the tag's offsetLeft before of prevTag
    const beforePrevTagOffsetLeft = prevTag.offsetLeft - tagAndTagSpacing.value
    if (afterNextTagOffsetLeft > $scrollWrapper.scrollLeft + $containerWidth) {
      $scrollWrapper.scrollLeft = afterNextTagOffsetLeft - $containerWidth
    } else if (beforePrevTagOffsetLeft < $scrollWrapper.scrollLeft) {
      $scrollWrapper.scrollLeft = beforePrevTagOffsetLeft
    }
  }
}
const scrollContainer = ref()
const handleMouseWheel = (e) => {
  if (e.wheelDelta || e.detail) {
    if (e.wheelDelta > 0 || e.detail < 0) {
      //当鼠标滚轮向上滚动时
      scrollContainer.value.scrollLeft(scrollWrapper.value.scrollLeft - 20)
    }
    if (e.wheelDelta < 0 || e.detail > 0) {
      //当鼠标滚轮向下滚动时
      scrollContainer.value.scrollLeft(scrollWrapper.value.scrollLeft + 20)
    }
  }
}

defineExpose({
  moveToTarget,
})
</script>
<style scoped lang="less">
:deep(.scroll-container) {
  overflow: hidden;
}
.contentWidth {
  height: @size-12;
  position: relative;
  overflow: hidden;
  white-space: nowrap;
  display: table;
}
</style>
